package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bjb.vr.common.dto.ResDevice;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.entity.SingleModeParameter;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Slf4j
@Component
public class VRSwitchSingleMultiMessageHandler implements MessageHandler {


    final String  LOCK_KEY = "res_device_multi_single_state";

    @Autowired
    ResDeviceService resDeviceService;

    @Autowired
    RedisUtil redisUtil;


    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {

        String dbLockKey = RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY + LOCK_KEY+ rabbitWebSocketMsg.getGroupCode();

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_VR_MULTI_SWITCH_SINGLE)){
            log.info("处理多台切换单机联网模式");

            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            JSONArray jsonArray = _jsonObject.getJSONArray("devUdids");
            int singleMode = _jsonObject.getInteger("singleMode");
            if(jsonArray!=null  && jsonArray.size()>0){

                Action action = new Action();
                action.setAction(MessageActionConstants.MESSAGE_ACTION_VR_SWITCH_SINGLE);

                String udid = "";
                Command command = new Command<>();
                SingleModeParameter parameter = new SingleModeParameter();
                for(int i=0 ; i<jsonArray.size() ; i++){

                    command = new Command<>();
                    udid = (String) jsonArray.get(i);
                    command.setUdid(udid);
                    parameter = new SingleModeParameter();
                    parameter.setSingleMode(singleMode);
                    command.setParameter(parameter);
                    action.setCommand(command);

                    rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
                    message.getCommand().setUdid(udid);
                    WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);
                }
                //设置为空消息，防止其他handler处理
                action.setAction(MessageActionConstants.MESSAGE_ACTION_EMPTY);
                rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));


                String value = String.valueOf(System.currentTimeMillis());
                if(redisUtil.lock(dbLockKey,value, RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY_EXPIRE_TIME)) {
                    List<ResDevice> resDeviceList = new ArrayList<>();
                    Date updateDate = new Date();
                    for(int i=0 ; i<jsonArray.size() ; i++){
                        ResDevice resDevice = new ResDevice();
                        resDevice.setMode(_jsonObject.getInteger("singleMode"));
                        resDevice.setUdid((String) jsonArray.get(i));
                        resDevice.setGmtModified(DateTimeUtil.getLocalDateTime(updateDate));
                        resDeviceList.add(resDevice);
                        UpdateWrapper<ResDevice> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.set("mode",resDevice.getMode());
                        updateWrapper.eq("udid", resDevice.getUdid());
                        resDeviceService.update(updateWrapper);

                    }
//                    resDeviceService.updateResDeviceBatch(resDeviceList);
                    redisUtil.unlock(dbLockKey, value);
                }
            }


        }

    }
}
